//
//  FishManager.m
//  fish
//
//  Created by 袁飞 on 12-8-28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FishManager.h"
#import "FUtil.h"
#import "Player.h"

@implementation FishConfig
@synthesize level = level_;
@synthesize name = name_;
@synthesize depth = depth_;
@synthesize img = img_;
@synthesize speed = speed_;
@synthesize vision = vision_;
@synthesize money = money_;
@synthesize gem = gem_;
@synthesize gem_gailv = gem_gailv_;

+(id)fishConfigWithDict:(NSMutableDictionary*)dict{
  return [[[FishConfig alloc] initWithDict:dict] autorelease];
}

-(id)initWithDict:(NSMutableDictionary*)dict{
  [super init];
  name_ = [dict objectForKey:@"img"];
  [name_ retain];
  img_ = [NSString stringWithFormat:@"%@.png", name_];
  [img_ retain];
  NSString* depth = [dict objectForKey:@"depth"];
  if ([depth length] > 0) {
    depth_ = [FUtil string2Point:depth];   
  }
  speed_ = [[dict objectForKey:@"speed"] intValue];
  vision_ = [[dict objectForKey:@"vision"] intValue];
  return self;
}
@end

@implementation FishData
@synthesize config = config_;
//@synthesize depth = depth_;
//@synthesize vision = vision_;
//@synthesize speed = speed_;

+(FishData*)FishDataWithConfig:(FishConfig*)cfg{
  return [[[FishData alloc] initWithConfig:cfg] autorelease];
}

-(id)initWithConfig:(FishConfig*)cfg{
  NSMutableDictionary * tmp = [NSMutableDictionary dictionary];
  [tmp setObject:[cfg name] forKey:@"fishno"];
  [super initWithXml:@"obj.xml" withDict:tmp];
  
  //speed_ = [cfg speed];
  //vision_ = [cfg vision];
  return self;
}

-(CGPoint)getWorldPosition{
  return [self position];
}

-(CGPoint)getLocalPosition{
  CGPoint p1 = [self position];
  CGPoint p2 = [[self parent] position];
  return ccp(p1.x+p2.x, p1.y+p2.y);
}
@end

@implementation FishManager
@synthesize fishes = fishes_;

static FishManager *_sharedFishManager = nil;
+ (FishManager *)sharedFishManager
{
  if (!_sharedFishManager) {
    _sharedFishManager = [[FishManager alloc] init];
    srand((unsigned int)time(NULL));
  }
  return _sharedFishManager;
}

-(id)init{
  [super init];
  [self load];
  return self;
}
-(void)load{
  fish_config_ = [NSMutableDictionary dictionary];
  [fish_config_ retain];
  fishes_ = [NSMutableArray array];
  [fishes_ retain];
  TBXML *xml = [[TBXML alloc] initWithXMLFile:@"config.xml"];
  if (!xml) {
    NSLog(@"FishManager --> load xml err");
  }
  TBXMLElement *root_element = [xml rootXMLElement];
  TBXMLElement *element = [TBXML childElementNamed:@"fish" parentElement:root_element];
  for (; element!=nil; ) {
    NSMutableDictionary* attrs = [FUtil getDictFromAttribute:element];    
    FishConfig *fc = [FishConfig fishConfigWithDict:attrs];
    if(fc){
      [fish_config_ setObject:fc forKey:[fc name]];
    }
    element = element->nextSibling;
  }
}

-(FishConfig*)getFishConfig:(NSString*)name{

}

-(NSMutableArray*)getFishConfigWithDepth:(int)depth{
  //for(NSString* s in dict){
  NSMutableArray *res = [NSMutableArray array];
  for(NSString* name in fish_config_){
    FishConfig* fc = [fish_config_ objectForKey:name];
    CGPoint p = [fc depth];
    if(depth>p.x && depth<p.y){
      [res addObject:fc];
    }
  }
  return res;
}

-(void)genFishes{ 
  int depth = [[Player sharedPlayer] depth];
/*  for(int i=0; i<10; i++){
    int rd = rand()%depth+1;
    //rd = 50;
    NSLog(@"depth --> %d", rd);
    int x = rand()%480;
    NSMutableArray* fcs = [self getFishConfigWithDepth:rd];
    int len = [fcs count];
    if (len == 0) {
      continue;
    }
    int ri = rand()%len;
    FishConfig* fc = [fcs objectAtIndex:ri];
    FishData* fd = [FishData FishDataWithConfig:fc];
    [fd setPosition:ccp(x, rd)];
    [fishes_ addObject:fd];
  }*/
}

@end
